# 弹窗组件（修复版）
import tkinter as tk
from tkinter import ttk, simpledialog
from db.database_manager import MySQLDatabaseManager

class MemoPopup(simpledialog.Dialog):
    def __init__(self, parent, date, db: MySQLDatabaseManager, memos=None):
        self.date = date
        self.db = db  # 显式注入数据库管理器
        self.memos = memos or []
        super().__init__(parent, title=f"{date.strftime('%Y-%m-%d')} 备忘录")

    def apply(self):
        """确认按钮回调（安全版）"""
        content = self.text.get("1.0", tk.END).strip()
        if content:
            self.db.add_memo(self.date, content)
    def body(self, master):
        ttk.Label(master, text="备忘录内容:").pack()
        self.text = tk.Text(master, height=10, width=40)
        self.text.pack(pady=5)
        if self.memos:
            self.text.insert('1.0', '\n'.join(self.memos))
        return self.text

